package com.gxgeek.algorithm.t2;

import java.util.Scanner;

/**
 * Created by gx on 2017/5/5.
 */
class DATA3 {
    String name;
    int age;
}

class StackType {
    static final int MAXLEN = 50;
    DATA3[] data = new DATA3[MAXLEN + 1];  //数据元素
    int top;                               // 栈顶


    StackType STInit() {//初始化栈
        StackType p;
        if ((p = new StackType()) != null) {
            p.top = 0;
            return p;
        }
        return null;
    }


    //    判断空栈
    boolean STIsEmpty(StackType s) {
        return s.top == 0;
    }

    //判断是否满栈
    boolean STIsFull(StackType s) {
        return s.top == MAXLEN;
    }

    //清空栈
    void STClear(StackType s) {
        s.top = 0;
    }

    //    释放空间
    void STFree(StackType s) {
        s = null;
    }


//    入栈

    int PushST(StackType s, DATA3 data) {
        if (s.top + 1 > MAXLEN) {
            System.out.println("栈溢出");
            return 0;
        }
        s.data[++s.top] = data;
        return 1;
    }

    //    出栈
    DATA3 Pop(StackType s) {
        if (s.top == 0) {
            System.out.println("栈为空");
            System.exit(0);
        }
        return s.data[s.top--];
    }


    //    读取节点
    DATA3 peekST(StackType s) {
        if (s.top == 0) {
            System.out.println("栈为空");
            System.exit(0);

        }
        return s.data[s.top];
    }


}


public class Stack {

    public static void main(String[] args) {
        StackType st = new StackType();
        DATA3 data1 = new DATA3();

        StackType stack = st.STInit();                        //初始化栈
        Scanner input = new Scanner(System.in);
        System.out.print("入栈操作：\n");
        System.out.println("输入姓名 年龄进行入栈操作:");
        do {
            DATA3 data = new DATA3();
            data.name = input.next();

            if (data.name.equals("0")) {
                break;                      //若输入0，则退出
            } else {
                data.age = input.nextInt();
                st.PushST(stack, data);
            }
        } while (true);

        String temp = "1";
        System.out.println("出栈操作:按任意非0键进行出栈操作:");
        temp = input.next();
        while (!temp.equals("0")) {
            data1 = st.Pop(stack);
            System.out.printf("出栈的数据是(%s,%d)\n", data1.name, data1.age);
            temp = input.next();
        }
        System.out.printf("出栈的数据是(%s,%d)\n", data1.name, data1.age);
//        st.STFree(st);                            //�ͷ�ջ��ռ�õĿռ�
//        System.out.println(st);
    }
}
